﻿using CVBanc.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace CVBanc.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult Login()
        {
            

            return View();
        }
        [HttpPost]
        public int Login2()
        {

            var username = Request["username"];
            var password = Request["password"];
            var id = CheckAuthentification(username, password); 
            if (id !=null)
            {
                Session["username"] = username;
                Session["password"] = password;
                Session["id"] = id; 
                return (int)id;
                //return RedirectToAction("Operations");
               // return null; 

            }

            //else
            //    return null;
            return -1; 
        }

        private int? CheckAuthentification(string username , string password)
        {
            
 
            using(var db = new Context()) 
	        {
                var user = db.Users.Where(x => x.Username == username && x.Password == password).FirstOrDefault();
                if (user !=null)
                    return user.Id;
                else
                    return null; 
                
	        }
            
        }
        [HttpPost]
        public string  Subscribe()
        {
         
            var firstname = Request["firstname"];
            var lastname = Request["lastname"];
            var username = Request["username"];
            var password = Request["password"];
            var eng = Request["engineering"]; 
            
            using (var db = new Context())
            {
                var user = new User { Firstname = firstname, Lastname = lastname , Username = username , Password = password, Engineering = eng };
                db.Users.Add(user);
                db.SaveChanges(); 
            }

            


            return "Inscription réussie"; 
        }

        //public ActionResult operations(string id)
       
        public ActionResult Operations()
        {
            return View(); 
        }
    }
}
